package com.xxx.ware.ware_manage_sys.controller.admin;

import com.xxx.ware.ware_manage_sys.bean.dto.AdminQueryDto;
import com.xxx.ware.ware_manage_sys.bean.dto.AdminSaveUpdateDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WebResultDto;
import com.xxx.ware.ware_manage_sys.bean.vo.AdminVo;
import com.xxx.ware.ware_manage_sys.bean.vo.ResultVo;
import com.xxx.ware.ware_manage_sys.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理员管理模块")
@RestController
@RequestMapping("/wms/admin")
@MapperScan("com.xxx.ware.ware_manage_sys.mapper")
@Slf4j
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @PostMapping("/list")
    @ApiOperation(value = "管理员列表", notes = "管理员列表", httpMethod = "POST")
    public ResultVo<WebResultDto<AdminVo>> list(@Validated @RequestBody AdminQueryDto adminQueryDto){
        try {
            log.info("管理员列表入参：{}", adminQueryDto);
            return ResultVo.data(adminService.getAdminList(adminQueryDto));
        } catch (Exception e) {
            log.error("管理员查询失败，对应失败原因：" + e);
            return ResultVo.fail("管理员查询失败");
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增管理员", notes = "新增管理员", httpMethod = "POST")
    public ResultVo<Boolean> saveAdmin(@Validated @RequestBody AdminSaveUpdateDto adminSaveUpdateDto){
        try {
            return ResultVo.data(adminService.saveAdmin(adminSaveUpdateDto));
        } catch (Exception e) {
            log.error("管理员新增失败，对应失败原因：" + e.getMessage());
            return ResultVo.fail("管理员新增失败");
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新管理员", notes = "更新管理员", httpMethod = "POST")
    public ResultVo<Boolean> updateAdmin(@Validated @RequestBody AdminSaveUpdateDto adminSaveUpdateDto){
        try {
            log.info("更新管理员，入参：{}", adminSaveUpdateDto);
            return ResultVo.data(adminService.updateAdmin(adminSaveUpdateDto));
        } catch (Exception e) {
            log.error("更新管理员信息失败，对应失败原因：" + e);
            return ResultVo.fail("更新管理员信息失败");
        }
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除管理员", notes = "删除管理员", httpMethod = "POST")
    public ResultVo<Boolean> removeAdmin(@RequestParam("id") Long id){
        try {
            return ResultVo.data(adminService.removeAdmin(id));
        } catch (Exception e) {
            log.warn("删除管理员异常，异常原因：" + e);
            return ResultVo.fail("删除管理员异常");
        }
    }
}
